<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用户登录</title>
    <link rel="stylesheet" href="lib/bootstrap.min.css">    
    <style>
        /* *{
            margin: 0;
            padding: 0;
        } */
        html{
            width: 100%;
            height: 100%;
        }
        body{
            background:url(lib/3.jpg) no-repeat;
            background-size: 100% 100%;
            position: fixed;
        }
        .box{
            width: 500px;
            height: 300px;
            border: 2px solid rgb(90, 85, 85);
            /* margin: 130px auto; */
            padding-top: 30px;
            padding-left: 50px;
            background-color: rgba(184, 182, 180,0.5);
            border-radius: 5px;
            position: fixed;
            top: 50%;
            left: 50%;
            transform:translate(-50%,-50%);
        }
    </style>
</head>
<body>
    <div class="box">
        <h2>用户登录</h2>
        <p style="margin-top: 30px;">
            <label for="username">用户名：</label>
            <input type="text" id="username">
        </p>
        <p style="margin-top: 10px;">
            <label for="password">密码：</label>
            <input type="password" id="password">
        </p>
        <p>
            <input type="checkbox" id="autoLogin">
            30天内免登录
        </p>
        <p style="display: inline-block;">
            <button id="login" class="btn btn-primary">登录</button>
        </p>
        <p style="display: inline-block; margin-left: 30px;">
            <button id="register" class="btn btn-primary">注册</button>
        </p>
    </div>

    <script src="lib/jquery.min.js"></script>
    <script src="common.js"></script>
    <script>
        $("#login").click(function(){
            let username = $("#username").val();
            let password = $("#password").val();            

            if(username && password){
                $.ajax({
                    url:`${address}/login`,
                    data:{
                        // username:username,
                        // password:password
                        username,
                        password
                    },
                    success(data){
                        // if(data === "3"){
                        //     alert("登陆失败")
                        // }
                        if(data === "2"){
                            alert("用户名或密码错误")
                        }
                        // if(data === "1"){
                        //     alert("哈哈哈哈")
                        // }
                        else{
                            // 30天内免登录
                            if($("#autoLogin")[0].checked){
                                let date = new Date();
                                date.setDate(date.getDate()+30);
                                document.cookie = `userInfo=${data};expires=${date}`;
                                // sessionStorage.setItem("userInfo",data)
                            }
                            sessionStorage.setItem("userInfo",data);
                            location.href = `${address}/studentList.html`;
                        }
                    }
                })
            }
        })

        $("#register").click(function(){
            location.href = `${address}/register.html`;
        })
    </script>

</body>
</html>